﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Document_Management
{
    public partial class ManualTool_LingYong_Insert : Form
    {
        string str, 手工具編號, 手工具名稱, 規格型號, 廠牌代號, 存放地點代號, 數量;
        bool b手工具Search, b手工具Insert, b手工具Edit, b手工具Del;
        static string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["庫存管理"].ConnectionString;
        public ManualTool_LingYong_Insert(bool b手工具Search,bool b手工具Insert,bool b手工具Edit,bool b手工具Del,string str, string 手工具編號,string 手工具名稱,string 規格型號,string 廠牌代號,string 存放地點代號,string 數量)
        {
            InitializeComponent();
            this.手工具編號 = 手工具編號;
            this.手工具名稱 = 手工具名稱;
            this.廠牌代號 = 廠牌代號;
            this.存放地點代號 = 存放地點代號;
            this.規格型號 = 規格型號;
            this.數量 = 數量;
            this.str = str;
            this.b手工具Del = b手工具Del;
            this.b手工具Edit = b手工具Edit;
            this.b手工具Insert = b手工具Insert;
            this.b手工具Search = b手工具Search;
        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {

        }

        private void bt_存檔_Click(object sender, EventArgs e)
        {
            if (str.Equals("借用"))
            {
                save("sp_手工具借用_insert");
                //this.Close();
                //ManualToolMnt_Borrow frm = new ManualToolMnt_Borrow(b手工具Search, b手工具Insert, b手工具Edit, b手工具Del);
                //frm.ShowDialog();
            }
            else
            { 
                save("sp_手工具領用_insert");
                //this.Close();
                //ManualToolMnt_Use frm = new ManualToolMnt_Use(b手工具Search, b手工具Insert, b手工具Edit, b手工具Del);
                //frm.ShowDialog();
            }

        }

        public void save(string procedure)
        {
            try
            {
                if (txt_手工具編號.Text.ToString() == null || txt_手工具編號.Text.ToString().Equals(""))
                    MessageBox.Show(str + "人代號還沒輸入資料");
                else
                {
                    SqlConnection con = new SqlConnection(connectionString);
                    con.Open();
                    SqlCommand cmd0;
                    String sql = "SELECT COUNT(*) FROM 手工具領用人,手工具領用部門 WHERE 領用人代號='" + txt_領用人.Text.ToString()
                                + "' AND 手工具領用人.領用部門代號=手工具領用部門.部門代號 AND 手工具領用部門.部門名稱='" + cb_領用部門代號.Text.ToString() + "'";
                    if (str.Equals("借用"))
                        sql = "SELECT COUNT(*) FROM 手工具借用人,手工具借用部門 WHERE 借用人代號='" + txt_領用人.Text.ToString()
                                + "' AND 手工具借用人.部門代號=手工具借用部門.部門代號 AND 手工具借用部門.部門名稱='" + cb_領用部門代號.Text.ToString() + "'"; ;
                    cmd0 = new SqlCommand(sql, con);
                    if ((int)cmd0.ExecuteScalar() == 0)
                    {
                        MessageBox.Show("Error! '" + txt_領用人.Text.ToString() + "' 不在 " + cb_領用部門代號.Text.ToString());
                    }
                    else
                    {
                        DateTime dt1, dt2;
                        dt1 = dateTime_領用日期.Value;
                        dt2 = dateTime_歸還日期.Value;
                        if (DateTime.Compare(dt1, dt2) > 0)
                        {
                            MessageBox.Show("輸入日期不對！");
                        }
                        else
                        {
                            int check1 = Int32.Parse(數量);
                            int check2 = Int32.Parse(txt_領用數量.Text.ToString());
                            if (check1 < check2 || check2 <= 0)
                            {
                                MessageBox.Show("0 (<) " + str + "數量（<=）" + check1);
                            }
                            else
                            {
                                cmd0 = new SqlCommand(procedure, con);
                                cmd0.CommandType = CommandType.StoredProcedure;
                                cmd0.Parameters.AddWithValue("@手工具編號", txt_手工具編號.Text.ToString());
                                cmd0.Parameters.AddWithValue("@受理人代號", txt_受理人.Text.ToString());
                                cmd0.Parameters.AddWithValue("@領用人代號", txt_領用人.Text.ToString());
                                cmd0.Parameters.AddWithValue("@數量", Int32.Parse(txt_領用數量.Text.ToString()));
                                cmd0.Parameters.AddWithValue("@備註", txt_備註.Text.ToString());
                                cmd0.Parameters.AddWithValue("@領用日期", dateTime_領用日期.Value);
                                cmd0.Parameters.AddWithValue("@歸還日期", dateTime_歸還日期.Value);
                                cmd0.ExecuteNonQuery();
                                //
                                cmd0 = new SqlCommand("sp_手工具_手工具領用_insert", con);
                                cmd0.CommandType = CommandType.StoredProcedure;
                                cmd0.Parameters.AddWithValue("@手工具編號", txt_手工具編號.Text.ToString());
                                cmd0.Parameters.AddWithValue("@數量", Int32.Parse(txt_領用數量.Text.ToString()));
                                cmd0.ExecuteNonQuery();
                                MessageBox.Show(str + "成功");
                                if (str.Equals("借用"))
                                {
                                    this.Close();
                                    ManualToolMnt_Borrow frm = new ManualToolMnt_Borrow(b手工具Search, b手工具Insert, b手工具Edit, b手工具Del);
                                    frm.ShowDialog();
                                }
                                else
                                {
                                    this.Close();
                                    ManualToolMnt_Use frm = new ManualToolMnt_Use(b手工具Search, b手工具Insert, b手工具Edit, b手工具Del);
                                    frm.ShowDialog();
                                }
                            }
                        }
                    }
                    con.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                return;
            }
        }

        public void Load_cb_領用部門代號()
        {
            SqlConnection connection = new SqlConnection(connectionString);
            connection.Open();

            string sqlQuery = "SELECT * FROM 手工具領用部門";
            if (str.Equals("借用"))
                sqlQuery = "SELECT * FROM 手工具借用部門";
            SqlCommand command = new SqlCommand(sqlQuery, connection);
            SqlDataReader sqlreader = command.ExecuteReader();
            while (sqlreader.Read())
            {
                cb_領用部門代號.Items.Add(sqlreader["部門名稱"].ToString());
            }
        }

        private void LingYong_Insert_Load(object sender, EventArgs e)
        {
            if (str.Equals("借用"))
            {
                this.Text = "借用管理";
                lb_人.Text = "借用人";
                lb_數量.Text = "借用數量";
                lb_日期.Text = "借用日期";
                lb_部門.Text = "借用部門";
                txt_領用人.Name = "txt_借用人";
            }
            txt_手工具編號.Text = 手工具編號;
            txt_規格型號.Text = 規格型號;
            txt_存放地點.Text = 存放地點代號;
            txt_廠牌.Text = 廠牌代號;
            txt_手工具名稱.Text = 手工具名稱;
            Load_cb_領用部門代號();
        }

        private void bt_離開_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void bt_受理人_search_Click(object sender, EventArgs e)
        {
            VehicleSearch frm = new VehicleSearch(txt_受理人);
            frm.ShowDialog();
        }

        private void bt_領用人_search_Click(object sender, EventArgs e)
        {
            VehicleSearch frm = new VehicleSearch(txt_領用人);
            frm.ShowDialog();
        }

        private void txt_受理人_TextChanged(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 手工具受理人 where 受理人代號=N'{0}'", txt_受理人.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lb_受理人.Text = "";
                else lb_受理人.Text = temp;
                con.Close();
            }
            catch (Exception ex) { }
        }

        private void txt_領用人_TextChanged(object sender, EventArgs e)
        {//領用人代號
            SqlConnection con = new SqlConnection(connectionString);
            try
            {
                con.Open();
                String temp = "";
                string strcmd = String.Format("select * from 手工具領用人 where 領用人代號=N'{0}'", txt_領用人.Text);
                SqlCommand cmd = new SqlCommand(strcmd, con);
                var x = cmd.ExecuteReader();
                while (x.Read()) temp = x[1].ToString();
                if (temp == "") lb_領用人.Text = "";
                else lb_領用人.Text = temp;
                con.Close();
            }
            catch (Exception ex) { }
        }

        private void txt_領用數量_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) || e.KeyChar == '.')
            {
                e.Handled = true;
                MessageBox.Show("很抱歉，請您輸入號碼", "錯誤", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
